﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageJobSubCategory : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_JOBSUBCATEGORY = "__JOBSUBCATEGORY::JOBSUBCATEGORY";
    private const string PROP_DATASRC_JOBCATEGORY = "__JOBCATEGORY::JOBCATEGORY";
    private const string PROP_LAYOUT = "__PAGEVIDEO::UIL";
    private const string PROP_SELECTED_JOBSUBCATEGORY_ID = "__JOBSUBCATEGORY::SJOBSUBCATEGORYID";

    #endregion

    #region Public Properties

    public JobSubCategoryCollection DataSourceJobSubCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBSUBCATEGORY] is JobSubCategoryCollection ? (JobSubCategoryCollection)SessionMgr.Session[PROP_DATASRC_JOBSUBCATEGORY] : (JobSubCategoryCollection)(SessionMgr.Session[PROP_DATASRC_JOBSUBCATEGORY] = new JobSubCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBSUBCATEGORY] = value; }
    }

    public int SelectedJobSubCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_JOBSUBCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_JOBSUBCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_JOBSUBCATEGORY_ID] = value; }
    }


    public JobCategoryCollection DataSourceJobCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] is JobCategoryCollection ? (JobCategoryCollection)SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] : (JobCategoryCollection)(SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] = new JobCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] = value; }
    }

    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }




    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceJobSubCategory = JobSubCategory.GetAllJobSubCategories();
            JobSubCategoryList1.DataBind(DataSourceJobSubCategory);
        }
    }

    #endregion

    #region Control: JobSubCategoryList

    protected void ucJobSubCategoryList_EditRequested(object sender, Controls_JobSubCategoryList.EditRequestedEventArgs evt)
    {
        SelectedJobSubCategoryID = evt.JSC.SubCategoryId;
        JobSubCategoryViewEdit1.DataBind(evt.JSC, DataSourceJobCategory);
        SetLayout(LayoutType.Edit);
    }

    protected void ucJobSubCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        JobSubCategoryViewEdit1.CancelEdit();
        JobSubCategory newJobSubCategory = JobSubCategory.CreateNew(DataSourceJobSubCategory);
        SelectedJobSubCategoryID = newJobSubCategory.SubCategoryId;
        DataSourceJobCategory = JobCategory.GetAllJobCategories();
        JobSubCategoryViewEdit1.DataBind(DataSourceJobSubCategory.FindByJobSubCategoryID(SelectedJobSubCategoryID), DataSourceJobCategory);
        SetLayout(LayoutType.Edit);
    }


    protected void ucJobSubCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceJobSubCategory = JobSubCategory.GetAllJobSubCategories();
        JobSubCategoryList1.DataSource = DataSourceJobSubCategory;
    }

    #endregion

    #region Control: JobSubCategoryViewEdit

    protected void ucJobSubCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        JobSubCategoryViewEdit1.DataSourceJobSubCategory = DataSourceJobSubCategory.FindByJobSubCategoryID(SelectedJobSubCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        JobSubCategoryViewEdit1.CancelEdit();
        // If we were adding a new video...then remove the incomplete/not added new jobsubcategory object from the collection.
        if (DataSourceJobSubCategory.FindByJobSubCategoryID(SelectedJobSubCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceJobSubCategory.Remove(DataSourceJobSubCategory.FindByJobSubCategoryID(SelectedJobSubCategoryID));
                SelectedJobSubCategoryID = (DataSourceJobSubCategory[0] != null ? DataSourceJobSubCategory[0].SubCategoryId : 0);
            }
            catch
            {
            }
        }
        JobSubCategoryList1.DataBind(DataSourceJobSubCategory);
        SetLayout(LayoutType.View);
    }

    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (JobSubCategoryViewEdit1.EndEdit())
        {
            JobSubCategory obj = DataSourceJobSubCategory.FindByJobSubCategoryID(SelectedJobSubCategoryID);
            JobSubCategory.Update(obj);

            // TODO: Set Feedback text...

            JobSubCategoryList1.DataBind(DataSourceJobSubCategory);
            SetLayout(LayoutType.View);

        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlJobSubCategoryList.Visible = false;
                pnlJobSubCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:

            case LayoutType.Search:

                pnlJobSubCategoryList.Visible = true;
                pnlJobSubCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
